<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
		<title>role_add</title>
		<link rel="shortcut icon" href="../images/my/favicon.ico" />
		<link rel="bookmark" href="../images/my/favicon.ico" />

		<link rel="stylesheet" href="../../../statics/layui/css/layui.css" />
		<link rel="stylesheet" href="../../../css/admin/common.css" />
		<link rel="stylesheet" href="../../../css/admin/update_my_base_info.css" />

		<link rel="stylesheet" href="../../metroStyle/metroStyle.css" />
		<style>
			.ztree * {
				font-size: 18px;
			}
			
			.ztree li a.curSelectedNode {}
		</style>
	</head>

	<body style="background: #F6F6F6;">
		<div class="layui-fluid">
			<div class="layui-row">
				<div class="layui-col-md12">
					<div class="layui-card layui-anim layui-anim-upbit">
						<div class="layui-card-header">评教标准管理
							<span class="btn_right">
								<button type="button" id="refresh" title="刷新" class="layui-btn layui-btn-normal layui-btn-xs"><i class="layui-icon layui-icon-refresh"></i></button>
							</span>
						</div>
						<div class="layui-card-body">
							<div class="layui-form-item layui-row">
								<div class="layui-col-md12 layui-col-sm12 layui-col-xs12">
									<ul id="treeDemo" class="ztree" style="border: 1px solid rgb(230,230,230);border-radius: 2px;"></ul>
									<div id="quanzhong" style="position: absolute;right: 2px;bottom: 2px;display: none;">
										<form class="layui-form" action="">
											<input type="hidden" name="eId" />
											<input name="eIsEnd" class="layui-input" lay-verify="number" style="width: 80px;color: red;display: inline-block;height: 28px;position: relative;top: 1px;" type="text" placeholder="权重" />
											<button lay-submit lay-filter="formDemo" class="layui-btn layui-btn-sm layui-btn-normal" type="button">保存权重</button>
										</form>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script type="text/javascript" src="../../../statics/jquery/jquery.min.js"></script>
		<script type="text/javascript" src="../../metroStyle/jquery.ztree.core.js"></script>
		<script type="text/javascript" src="../../metroStyle/jquery.ztree.excheck.js"></script>
		<script type="text/javascript" src="../../metroStyle/jquery.ztree.exedit.min.js"></script>

		<script type="text/javascript" src="../../../statics/layui/layui.js"></script>
		<script type="text/javascript" src="../../../statics/lay/system/admin/public.js"></script>
		<script>
			var setting = {
				view: {
					addHoverDom: addHoverDom,
					removeHoverDom: removeHoverDom,
					selectedMulti: false
				},
				edit: {
					enable: true,
					editNameSelectAll: true,
					removeTitle: "删除",
					renameTitle: "编辑",
					showRemoveBtn: showRemoveBtn,
					showRenameBtn: true
				},
				data: {
					key: {
						title: "eTitle",
						name: "eTitle",
					},
					simpleData: {
						enable: true,
						idKey: "eId",
						pIdKey: "eParentId",
						rootPId: 0
					}
				},
				callback: {
					beforeRemove: beforeRemove,
					onRemove: onRemove,
					onRename: onRename,
					onClick: zTreeOnClick
				}
			};
			var Jquery = $;
			var treeNode;

			var newCount = -1;

			function addHoverDom(treeId, treeNode) {
				if(Jquery && newCount > -1) {
					var sObj = $("#" + treeNode.tId + "_span");
					if(treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
					var addStr = "<span class='button add' id='addBtn_" + treeNode.tId +
						"' title='添加' onfocus='this.blur();'></span>";
					sObj.after(addStr);
					var btn = $("#addBtn_" + treeNode.tId);
					if(btn) btn.bind("click", function() {
						var zTree = $.fn.zTree.getZTreeObj("treeDemo");
						newCount++;
						var data = {
							eId: newCount,
							eParentId: treeNode.eId,
							eTitle: "新问题" + newCount
						}
						zTree.addNodes(treeNode, data);
						addEvaluate(data)
						return false;
					});
				}
			};

			function removeHoverDom(treeId, treeNode) {
				if(Jquery) {
					$("#addBtn_" + treeNode.tId).unbind().remove();
				}
			};

			function showRemoveBtn(treeId, treeNode) {
				return treeNode.eParentId == 0 ? false : true;
			}

			function beforeRemove(treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				zTree.selectNode(treeNode);
				return confirm("确认删除  -- " + treeNode.eTitle + " 吗？");
			}

			function zTreeOnClick(e, treeId, treeNode) {
				var eIsEnd = treeNode.eIsEnd;
				var eId = treeNode.eId;
				$("input[name='eIsEnd']").val(eIsEnd)
				$("input[name='eId']").val(eId)
				$("#quanzhong").show()
			}

			function onRemove(e, treeId, treeNode) {
				var eId = treeNode.eId;
				if(Jquery) {
					Jquery.ajax({
						url: IP + '/api/teacher_eav/evaluate/' + eId,
						type: 'DELETE',
						success: function(result, status, xhr) {
							layer.closeAll('loading');
							layer.msg(result.message, {
								icon: 1,
								time: 1000
							});
						}
					});
				}
			}

			function onRename(e, treeId, treeNode, isCancel) {
				var data = {
					eId: treeNode.eId,
					eTitle: treeNode.eTitle
				}
				if(Jquery) {
					Jquery.ajax({
						url: IP + '/api/teacher_eav/evaluate',
						type: 'PUT',
						data: JSON.stringify(data),
						success: function(result, status, xhr) {
							layer.closeAll('loading');
							layer.msg(result.message, {
								icon: 1,
								time: 1000
							});
						}
					});
				}
			}

			function addEvaluate(data) {
				if(Jquery) {
					Jquery.ajax({
						url: IP + '/api/teacher_eav/evaluate',
						type: 'POST',
						data: JSON.stringify(data),
						success: function(result, status, xhr) {
							layer.closeAll('loading');
							layer.msg(result.message, {
								icon: 1,
								time: 1000
							});
						}
					});
				}
			}
		</script>
		<script>
			layui.use(['layer', 'form'], function() {
				var layer = layui.layer,
					$ = layui.$,
					form = layui.form;
				treeNode = getEvaluates();
				newCount = (treeNode[treeNode.length - 1]).eId;
				var zTreeObj = Jquery.fn.zTree.init($("#treeDemo"), setting, treeNode);
				//表单监听提交
				$("#quanzhong").hide()
				$("input").val("")
				form.on('submit(formDemo)', function(data) {
					var dict_data = data.field;
					dict_data = JSON.stringify(dict_data)
					$.ajax({
						url: IP + '/api/teacher_eav/evaluate',
						type: 'PUT',
						data: dict_data,
						success: function(result, status, xhr) {
							layer.closeAll('loading');
							layer.msg(result.message, {
								icon: 1,
								time: 1000
							});
						}
					});
					return false;
				});
			});
		</script>
	</body>

</html>